import { expandableTypes } from '../config/typeConfig'

/**
 * 创建字段
 * @param {string} type - 字段类型
 * @returns {Object} - 返回对应类型的字段配置对象
 */
export const createField = (type = 'string') => {
  // 基础字段属性
  const baseField = {
    type,
    fieldName: '',
    apipiost_allow_null: true,
    description: '',
    expanded: false,
    required: false,
    example: '',
    mock: ''
  }

  // 根据类型添加特定属性
  switch (type) {
    case 'string':
      return {
        ...baseField,
        minLength: 0,
        maxLength: 0,
        format: '',
        pattern: {
          enabled: false,
          value: ''
        }
      }

    case 'number':
      return {
        ...baseField,
        default: undefined,
        minimum: undefined,
        maximum: undefined,
        exclusiveMinimum: false,
        exclusiveMaximum: false
      }

    case 'integer':
      return {
        ...baseField,
        default: undefined,
        minimum: undefined,
        maximum: undefined,
        exclusiveMinimum: false,
        exclusiveMaximum: false
      }

    case 'boolean':
      return {
        ...baseField,
        default: undefined
      }

    case 'array':
      return {
        ...baseField,
        uniqueItems: false,
        minItems: 0,
        maxItems: 0,
        properties: {},
        APIPOST_ORDERS: [],
        items: {
          type: 'object',
          properties: {},
          APIPOST_ORDERS: [],
          required: []
        }
      }

    case 'object':
      return {
        ...baseField,
        properties: {},
        APIPOST_ORDERS: []
      }

    case 'null':
      return {
        ...baseField
      }

    case 'oneof':
    case 'anyof':
    case 'allof':
      return {
        ...baseField,
        properties: {},
        APIPOST_ORDERS: []
      }

    default:
      return baseField
  }
}

/**
 * 查找父节点
 * @param {Object} rootNode - 根节点
 * @param {Object} targetNode - 目标节点
 * @returns {Object|null} - 返回包含父节点和字段名的对象，如果未找到则返回null
 */
export const findParentNode = (rootNode, targetNode) => {
  for (const fieldName of rootNode.APIPOST_ORDERS) {
    if (rootNode.properties[fieldName] == targetNode.node) {
      return { parent: rootNode, fieldName };
    }
    if (expandableTypes.includes(rootNode.properties[fieldName].type)) {
      const result = findParentNode(rootNode.properties[fieldName], targetNode);
      if (result) return result;
    }
  }
  return null;
}

/**
 * 判断是否可以添加子节点
 * @param {string} type - 节点类型
 * @returns {boolean} - 返回是否可以添加子节点
 */
export const canAddChildren = (type, node) => {
  if (type == 'array') {
    return Object.keys(node.properties).length === 0;
  } else {
    return expandableTypes.includes(type)
  }
}

/**
 * 获取类型的默认值
 * @param {string} type - 字段类型
 * @returns {any} - 返回对应类型的默认值
 */
export const getDefaultValue = (type) => {
  switch (type) {
    case 'string':
      return ''
    case 'number':
    case 'integer':
      return 0
    case 'boolean':
      return false
    case 'array':
      return []
    case 'object':
      return {}
    case 'null':
      return null
    default:
      return undefined
  }
} 